package com.gmail.fanvaron.Commands;

import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;

import com.gmail.fanvaron.Main.MarvMonsterPack;

public class BuyCommand implements CommandExecutor {

    MarvMonsterPack plugin;

    public BuyCommand(MarvMonsterPack instance) {
        plugin = instance;
    }

    @SuppressWarnings("deprecation")
    public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
        
        if(args.length != 4)
            return false;
        
        Player player = Bukkit.getServer().getPlayer(args[0]);
        ItemStack[] items = player.getInventory().getContents();
        boolean hasEmptySlot = false;
        for (ItemStack is : items) {
            if (is == null) {
                hasEmptySlot = true;
            }
        }
        if (hasEmptySlot) {
            if (player.getInventory().contains(Material.EMERALD, Integer.parseInt(args[3]))) {
                player.getInventory().addItem(
                        new ItemStack[] { new ItemStack(Material.getMaterial(Integer.parseInt(args[1])), Integer
                                .parseInt(args[2])) });
                player.getInventory().removeItem(
                        new ItemStack[] { new ItemStack(Material.EMERALD, Integer.parseInt(args[3])) });
            } else {
                player.sendMessage("You cannot afford that!");
            }
        } else {
            player.sendMessage("No room for that in your inventory!");
        }
        return true;
    }
}
